package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController{
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @RequestMapping(value = "/list",name = "分页查询所有的用户")
    //分页查询所有数据
    public String findAllById(@RequestParam(value = "page",defaultValue = "1")int pageNum,@RequestParam(defaultValue = "5")int pages){
        PageInfo<User> pageInfo = userService.findPages(pageNum, pages, getCompanyId());
        request.setAttribute("page",pageInfo);
        return "system/user/user-list";
    }
    @RequestMapping(value = "/toAdd",name = "跳转到添加用户界面")
    //跳转到添加界面
    public String toAdd(){
        List<Dept> list = deptService.findAllById(getCompanyId());
        request.setAttribute("deptList",list);
        return "system/user/user-add";
    }
    @RequestMapping(value = "/edit",name = "新建和编辑用户")
    //新建和编辑数据
    public String edit(User user){
        if(StringUtils.isBlank(user.getId())) {
            user.setCreateTime(new Date());
            user.setId(UUID.randomUUID().toString());
            user.setCompanyId(getCompanyId());
            //String password = user.getPassword();//获取用户的密码
            //session.setAttribute("password",password);
            user.setCompanyName(getCompanyName());
            userService.insert(user);
        }else {
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }
    @RequestMapping(value = "delete",name = "删除指定用户")
    //删除数据
    public String delete(String id){
        userService.deleteById(id);
        return "redirect:/system/user/list.do";
    }
    @RequestMapping(value = "toUpdate",name = "跳转到用户编辑界面")
    //跳转到编辑界面
    public String toUpdate(String id){
        List<Dept> list = deptService.findAllById(getCompanyId());
        request.setAttribute("deptList",list);
        User user=userService.findUserById(id);
        request.setAttribute("user",user);
        return "system/user/user-add";
    }
    //用户角色管理
    @RequestMapping(value = "/roleList",name = "用户角色管理")
    public String roleList(String id){
        //获取当前公司对应的所有角色并放入域中
        List<Role> list = roleService.findAll(getCompanyId());
        User user = userService.findUserById(id);
        //查询当前用户
        request.setAttribute("user",user);
        request.setAttribute("roleList",list);
        //查询当前用户id在pe-role-user表中对应的角色有几个并查询出来
        List<String> roles=userService.findAllRoleIdByUserId(id);
        String s="";
        for (String role : roles) {
            s+=role+",";
        }
        System.out.println("封装好的字符串是："+s);
        request.setAttribute("userRoleStr",s);
        return "system/user/user-role";
    }
    @RequestMapping(value = "/changeRole",name = "修改用户角色")
    public String changeRole(String userid,String[] roleIds){
        userService.deleteRoleByUserId(userid);
        for (String roleId : roleIds) {
            userService.insertRoleIdByUserId(userid,roleId);
        }
        return "redirect:/system/user/list.do";
    }
}
